/**
 * https://vitejs.dev/config/
 */
import type { ConfigEnv, UserConfig } from 'vite';
import { createVitePlugins } from './build';

import fs from 'fs';
import path from 'path';
import dotenv from 'dotenv';

export default ({ command }: ConfigEnv): UserConfig => {
  const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), './.env')));
  const isBuild = command === 'build';
  return {
    envPrefix: 'UNIAPP_',
    plugins: createVitePlugins(env, isBuild),
    server: {
      host: true,
      port: 8080,
      // https: {},
      proxy: {
        '/api': {
          target: isBuild ? env.UNIAPP_TRIAL_BASE_URL : env.UNIAPP_DEVELOP_BASE_URL,
          changeOrigin: true,
          secure: false,
          ws: false,
          rewrite: path => path.replace(/^\/api/, '')
        },
      },
    },
    define: { "process.env": {} }
  };
};
